#include "Qt3DPicker.h"
#include "OsgUtil.h"
#include "CmdManager.h"
#include "Scene.h"
#include "SendPointHandlerBase.h"
#include "Operation/OpAdd.h"
#include "OSGNodeMask.h"
#include "LineStrip3D.h"
#include "Plane.h"
#include "PolygonGeneratorHandler.h"
#include "QtOSGWidget.h"
#include "PolygonGenerator.h"


PolygonGeneratorHandler::PolygonGeneratorHandler()
{
}

PolygonGeneratorHandler::~PolygonGeneratorHandler()
{
}

void PolygonGeneratorHandler::input(const QVariant& data, const QString& rawInput, CommandSource cs)
{
	if (status() == 0)
	{
		auto scene = getScene();
		auto children = scene->getChildren();
		if (children.size() > 0)
		{
			std::vector<std::shared_ptr<LineStrip3D>> lines;
			for (auto child : children)
			{
				auto line3D = std::dynamic_pointer_cast<LineStrip3D>(child);
				if (line3D)
				{
					lines.push_back(line3D);
				}
			}
			auto polygons = generatePolygons(lines, scene);
			if (polygons.size() > 0)
			{
				std::set<std::shared_ptr<INode>> results;
				for (const auto& polygon : polygons)
				{
					results.insert(polygon);
				}
				auto undoStack = scene->undoStack();
				undoStack->push(new OpAdd(results));
			}
		}
		emit finished();
		return;
	}
}